package utils

import (
	"crypto/rand"
	"encoding/base64"
	"io"
	r "math/rand"
	"time"
	"unsafe"
)

// 生成Guid字符串
func NewGuid() string {
	b := make([]byte, 48)

	if _, err := io.ReadFull(rand.Reader, b); err != nil {
		return ""
	}
	return Md5Encrypt(base64.URLEncoding.EncodeToString(b))
}

// 生成随机数字的字符串
func NewRandomNum(n int) string {
	return string(NewRandomBytes(n, "0123456789"))
}

// 生成随机字符串
// keywords 为可选参数，只取[0]，指定随机数取值范围
func NewRandomBytes(n int, keywords ...string) []byte {
	ks := ""
	if len(keywords) > 0 {
		ks = keywords[0]
	}
	alphabets := []byte(ks)

	const alphanum = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"
	var bytes = make([]byte, n)
	var randby bool
	if num, err := rand.Read(bytes); num != n || err != nil {
		r.Seed(time.Now().UnixNano())
		randby = true
	}
	for i, b := range bytes {
		if len(alphabets) == 0 {
			if randby {
				bytes[i] = alphanum[r.Intn(len(alphanum))]
			} else {
				bytes[i] = alphanum[b%byte(len(alphanum))]
			}
		} else {
			if randby {
				bytes[i] = alphabets[r.Intn(len(alphabets))]
			} else {
				bytes[i] = alphabets[b%byte(len(alphabets))]
			}
		}
	}
	return bytes
}

func SubStr(str string, begin, length int) string {
	// 将字符串的转换成[]rune
	rs := []rune(str)
	lth := len(rs)

	// 简单的越界判断
	if begin < 0 {
		begin = 0
	}
	if begin >= lth {
		begin = lth
	}
	end := begin + length
	if end > lth {
		end = lth
	}

	// 返回子串
	return string(rs[begin:end])
}

//高性能字符串转字节
func StringToByte(s string) []byte {
	x := (*[2]uintptr)(unsafe.Pointer(&s))
	h := [3]uintptr{x[0], x[1], x[1]}
	return *(*[]byte)(unsafe.Pointer(&h))
}

//高性能字节转字符串
func ByteToString(b []byte) string {
	return *(*string)(unsafe.Pointer(&b))
}
